package com.surpassun.emall.service.impl;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.surpassun.emall.model.Address;
import com.surpassun.emall.repository.AddressRepository;
import com.surpassun.emall.service.AddressService;
import com.surpassun.emall.util.Validator;

/**
 *
 * @author Denis
 */
@Service
public class AddressServiceImpl extends AbstractServiceImpl<Address> implements AddressService {

	@Inject
	private AddressRepository addressRepository;
	
    public AddressServiceImpl() {
    	super(Address.class);
    }
    
	@Override
	public void addAddress(long countryId, long provinceId, long cityId,
			String postCode, String street, String complememt) {
		if (Validator.isNotNull(street) && Validator.isNotNull(postCode)) {
			Address address = new Address(countryId, provinceId, cityId, postCode, street, complememt);
			addressRepository.add(address);
		}
	}

	@Override
	public void updateAddress(long addressId, long countryId, long provinceId, long cityId,
			String postCode, String street, String complememt) {
		Address address = addressRepository.get(addressId);
		if (Validator.isNotNull(address) &&
			Validator.isNotNull(street) && Validator.isNotNull(postCode)) {
			address.setCountryId(countryId);
			address.setProvinceId(provinceId);
			address.setCityId(cityId);
			address.setPostCode(postCode);
			address.setStreet(street);
			address.setComplememt(complememt);
			addressRepository.update(address);
		}
	}
}